struct ev_loop
{
    ev_tstamp ev_rt_now;
    ev_tstamp now_floor;
    ev_tstamp mn_now;
    ev_tstamp rtmn_diff;
    W * rfeeds;
    int rfeedmax;
    int rfeedcnt;
    ANPENDING *pendings [((((0x7f) & 4) ? +2 : 0) - (((0x7f) & 4) ? -2 : 0) + 1)];
    int pendingmax [((((0x7f) & 4) ? +2 : 0) - (((0x7f) & 4) ? -2 : 0) + 1)];
    int pendingcnt [((((0x7f) & 4) ? +2 : 0) - (((0x7f) & 4) ? -2 : 0) + 1)];
    int pendingpri;
    ev_prepare pending_w;
    ev_tstamp io_blocktime;
    ev_tstamp timeout_blocktime;
    int backend;
    int activecnt;
    sig_atomic_t volatile loop_done;
    int backend_fd;
    ev_tstamp backend_mintime;
    void (*backend_modify)(struct ev_loop *loop, int fd, int oev, int nev);
    void (*backend_poll)(struct ev_loop *loop, ev_tstamp timeout);
    ANFD * anfds;
    int anfdmax;
    int evpipe [2];
    ev_io pipe_w;
    sig_atomic_t volatile pipe_write_wanted;
    sig_atomic_t volatile pipe_write_skipped;
    pid_t curpid;
    char postfork;
    void * vec_ri;
    void * vec_ro;
    void * vec_wi;
    void * vec_wo;
    int vec_max;
    struct pollfd * polls;
    int pollmax;
    int pollcnt;
    int * pollidxs;
    int pollidxmax;
    struct epoll_event * epoll_events;
    int epoll_eventmax;
    int * epoll_eperms;
    int epoll_epermcnt;
    int epoll_epermmax;
    int * fdchanges;
    int fdchangemax;
    int fdchangecnt;
    ANHE * timers;
    int timermax;
    int timercnt;
    ANHE * periodics;
    int periodicmax;
    int periodiccnt;
    ev_idle **idles [((((0x7f) & 4) ? +2 : 0) - (((0x7f) & 4) ? -2 : 0) + 1)];
    int idlemax [((((0x7f) & 4) ? +2 : 0) - (((0x7f) & 4) ? -2 : 0) + 1)];
    int idlecnt [((((0x7f) & 4) ? +2 : 0) - (((0x7f) & 4) ? -2 : 0) + 1)];
    int idleall;
    struct ev_prepare ** prepares;
    int preparemax;
    int preparecnt;
    struct ev_check ** checks;
    int checkmax;
    int checkcnt;
    struct ev_fork ** forks;
    int forkmax;
    int forkcnt;
    struct ev_cleanup ** cleanups;
    int cleanupmax;
    int cleanupcnt;
    sig_atomic_t volatile async_pending;
    struct ev_async ** asyncs;
    int asyncmax;
    int asynccnt;
    int fs_fd;
    ev_io fs_w;
    char fs_2625;
    ANFS fs_hash [((0x7f) & 2) ? 16 : 1];
    sig_atomic_t volatile sig_pending;
    int sigfd;
    ev_io sigfd_w;
    sigset_t sigfd_set;
    unsigned int origflags;
    unsigned int loop_count;
    unsigned int loop_depth;
    void * userdata;
    void (*release_cb)(struct ev_loop *loop) ;
    void (*acquire_cb)(struct ev_loop *loop) ;
    void (*invoke_cb) (struct ev_loop *loop);
};

typedef struct ev_watcher
{
    // 表示 watcher 是否活跃，active = 1 表示还没被 stop 掉  
    int active;  
    // 存储 watcher 在 pendings 中的索引 大于零表示还没被处理
    // watcher 的回调函数被调用后，会设置为 0
    int pending;
    int priority; 
    // 回调所需数据
    void *data; 
    void (*cb)(struct ev_loop *loop, struct ev_watcher *w, int revents);
} ev_watcher;

typedef struct ev_watcher_list
{
    int active;
    int pending;
    int priority;
    void *data;
    void (*cb)(struct ev_loop *loop, struct ev_watcher_list *w, int revents);

    struct ev_watcher_list *next;
} ev_watcher_list;

typedef struct ev_io
{
    //ev_watcher
    //ev_watcher_list
    int active;
    int pending;
    int priority;
    void *data;
    void (*cb)(struct ev_loop *loop, struct ev_io *w, int revents);
    struct ev_watcher_list *next;

    int fd;
    int events;
} ev_io;


ev_io stdin_watcher;
static void stdin_cb (struct ev_loop *loop, ev_io *w, int revents)
{
    puts ("stdin ready");
    ev_io_stop (loop, w);
    ev_break (loop, EVBREAK_ALL);
}

int main(void)
{
    struct ev_loop *loop = ev_default_loop (0);


    //#define ev_init(ev,cb_) do {			\
    //  ((ev_watcher *)(void *)(ev))->active  =	\
    //  ((ev_watcher *)(void *)(ev))->pending = 0;	\
    //  ev_set_priority ((ev), 0);			\
    //  ev_set_cb ((ev), cb_);			\
    //} while (0)
    //#define ev_io_set(ev,fd_,events_)            do { (ev)->fd = (fd_); (ev)->events = (events_) | EV__IOFDSET; } while (0)
    //#define ev_io_init(ev,cb,fd,events)          do { ev_init ((ev), (cb)); ev_io_set ((ev),(fd),(events)); } while (0)

    // ev_io_init (&stdin_watcher, stdin_cb, /*STDIN_FILENO*/ 0, EV_READ);
    // init struct ev_io
    do {
        do {
            ((ev_watcher *)(void *)((&stdin_watcher)))->active = ((ev_watcher *)(void *)((&stdin_watcher)))->pending = 0;
            ( (ev_watcher *)(void *)(((&stdin_watcher))))->priority = (0);
            (((&stdin_watcher)))->cb = ((stdin_cb));
        } while (0);

        do { 
            ((&stdin_watcher))->fd = ((0));
            ((&stdin_watcher))->events = ((EV_READ)) | EV__IOFDSET;
        } while (0);
    } while (0);

    ev_io_start (loop, &stdin_watcher);
    ev_run (loop, 0);
    return 0;
}

typedef ev_watcher *W;
typedef ev_watcher_list *WL;
void ev_ref (struct ev_loop *loop)
{
  ++(loop->activecnt);
}
static void pri_adjust (struct ev_loop *loop, W w)
{
  int pri = (+(((ev_watcher *)(void *)(w))->priority));
  pri = pri < (((0x7f) & 4) ? -2 : 0) ? (((0x7f) & 4) ? -2 : 0) : pri;
  pri = pri > (((0x7f) & 4) ? +2 : 0) ? (((0x7f) & 4) ? +2 : 0) : pri;
  ( (ev_watcher *)(void *)(w))->priority = (pri);
}
static void ev_start (struct ev_loop *loop, W w, int active)
{
  pri_adjust (loop, w);
  w->active = active;
  ev_ref (loop);
}

static void wlist_add (ev_watcher_list** head, ev_watcher_list* elem)
{
  elem->next = *head;
  *head = elem;
}

//loop->anfds
typedef struct
{
  WL head;
  unsigned char events; /* the events watched for */
  unsigned char reify; /* flag set when this ANFD needs reification (EV_ANFD_REIFY, EV__IOFDSET) */
  unsigned char emask; /* the epoll backend stores the actual kernel mask in here */
  unsigned char unused;
  unsigned int egen;  /* generation counter to counter epoll bugs */
} ANFD;

static void fd_change (struct ev_loop *loop, int fd, int flags)
{
    unsigned char reify = (loop->anfds)[fd].reify;
    (loop->anfds)[fd].reify |= flags;
    //reify之前是0，要将fd加到fdchanges数组中
    if (!reify) {
        ++(loop->fdchangecnt);
        if (loop->fdchangecnt > loop->fdchangemax) {
            int ocur_ = loop->fdchangemax;
            loop->fdchanges = (int *)array_realloc (sizeof(int), loop->fdchanges, &(loop->fdchangemax), loop->fdchangecnt);
        };
        (loop->fdchanges)[loop->fdchangecnt - 1] = fd;
    }
}
void ev_io_start (struct ev_loop *loop, ev_io *w)
{
    int fd = w->fd;
    //如果对一个ev_io已经调用过ev_io_start，不再重复调用
    if ((0 + ((ev_watcher *)(void *)(w))->active) == 1) return;

    //设置事件状态为启动
    ev_start (loop, (ev_watcher*)w, 1);

    //判断当前分配的fd数组是否能放该fd，若不能，分配更多的空间给fd数组(realloc)。
    //这是一个宏，anfds实际上就是loop->anfds，存储了和该loop关联的ANFD数组。
    //anfdmax是当前数组能放的最大fd，array_init_zero也是个宏，对新分配的空间memeset成0。
    if (fd + 1 > loop->anfdmax) {
        int ocur_ = loop->anfdmax;
        //array_realloc(单个大小, 头指针, 个数, 最小个数) 
        loop->anfds = (ANFD *)array_realloc (sizeof (ANFD), loop->anfds, &(loop->anfdmax), (fd + 1));
        memset (loop->anfds + ocur_, 0, sizeof (*(loop->anfds + ocur_)) * (loop->anfdmax - ocur_));
    }

    //将该io事件添加到该fd对应结构体的事件链表里，使用头插法
    wlist_add (&(loop->anfds)[fd].head, (ev_watcher_list*)w);

    //将该fd加到fdchanges数组里，fdchanges保存了事件监听状态改变的fd
    //事件驱动器会在合适的时候遍历fdchanges数组，根据使用的后台IO复用机制应用更改，比如epoll_ctl
    fd_change (loop, fd, w->events & EV__IOFDSET | 1);

    //start结束，取消该事件的IOFDSET标记（这个标记是在init时加的）
    w->events &= ~EV__IOFDSET;
}

void ev_unref (struct ev_loop *loop)
{
  --(loop->activecnt);
}
static void ev_stop (struct ev_loop *loop, W w)
{
  ev_unref (loop);
  w->active = 0;
}
static void clear_pending (struct ev_loop *loop, W w)
{
    if (w->pending)
    {
        ((loop)->pendings) [(((W)w)->priority - (((0x7f) & 4) ? -2 : 0))][w->pending - 1].w = (W)&((loop)->pending_w);
        w->pending = 0;
    }
}
//从链表中删除一个节点，十分经典的方法
void wlist_del (WL *head, WL elem)
{
    while (*head)
    {
        if (expect_true (*head == elem))
        {
            *head = elem->next;
            break;
        }
        head = &(*head)->next;
    }
}
void ev_io_stop (struct ev_loop *loop, ev_io *w)
{
    //如果该事件正在pending(等待执行的事件)中，从pending列表中移除该事件。
    //这里的一个技巧是不用真的移除掉(数组删除复杂度O(n))，只要将pending列表对应位置的指针指向一个空事件就可以了。
    clear_pending (loop, (W)w);
    if (w->active == 0) return;
    wlist_del (&((loop)->anfds)[w->fd].head, (WL)w);
    ev_stop (loop, (W)w);
    //将fd加到fdchanges数组中，只设置REIFY标记，表示有改动(若什么标记都不设置该fd不会放入fdchanges)
    //之后事件驱动器扫描fdchanges数组会发现该fd不再监听任何事件，作出相应操作
    fd_change (loop, w->fd, 1);
}











typedef struct ev_watcher_time
{
    int active;
    int pending;
    int priority;
    void *data;
    void (*cb)(struct ev_loop *loop, struct ev_watcher_time *w, int revents);
    ev_tstamp at;
} ev_watcher_time;

typedef struct ev_timer
{
    int active;
    int pending;
    int priority;
    void *data;
    void (*cb)(struct ev_loop *loop, struct ev_timer *w, int revents);
    ev_tstamp at;
    ev_tstamp repeat;
} ev_timer;

typedef struct ev_periodic
{
    int active;
    int pending;
    int priority;
    void *data;
    void (*cb)(struct ev_loop *loop, struct ev_periodic *w, int revents);
    ev_tstamp at;
    ev_tstamp offset;
    ev_tstamp interval;
    ev_tstamp (*reschedule_cb)(struct ev_periodic *w, ev_tstamp now) throw();
} ev_periodic;

typedef struct ev_signal
{
    int active;
    int pending;
    int priority;
    void *data;
    void (*cb)(struct ev_loop *loop, struct ev_signal *w, int revents);
    struct ev_watcher_list *next;
    int signum;
} ev_signal;

typedef struct ev_child
{
    int active;
    int pending;
    int priority;
    void *data;
    void (*cb)(struct ev_loop *loop, struct ev_child *w, int revents);
    struct ev_watcher_list *next;
    int flags;
    int pid;
    int rpid;
    int rstatus;
} ev_child;

typedef struct stat ev_statdata;
typedef struct ev_stat
{
    int active;
    int pending;
    int priority;
    void *data;
    void (*cb)(struct ev_loop *loop, struct ev_stat *w, int revents);
    struct ev_watcher_list *next;
    ev_timer timer;
    ev_tstamp interval;
    const char *path;
    ev_statdata prev;
    ev_statdata attr;
    int wd;
} ev_stat;

typedef struct ev_idle
{
    int active;
    int pending;
    int priority;
    void *data;
    void (*cb)(struct ev_loop *loop, struct ev_idle *w, int revents);
} ev_idle;

typedef struct ev_prepare
{
    int active;
    int pending;
    int priority;
    void *data;
    void (*cb)(struct ev_loop *loop, struct ev_prepare *w, int revents);
} ev_prepare;

typedef struct ev_check
{
    int active;
    int pending;
    int priority;
    void *data;
    void (*cb)(struct ev_loop *loop, struct ev_check *w, int revents);
} ev_check;

typedef struct ev_fork
{
    int active;
    int pending;
    int priority;
    void *data;
    void (*cb)(struct ev_loop *loop, struct ev_fork *w, int revents);
} ev_fork;

typedef struct ev_cleanup
{
    int active;
    int pending;
    int priority;
    void *data;
    void (*cb)(struct ev_loop *loop, struct ev_cleanup *w, int revents);
} ev_cleanup;

typedef struct ev_embed
{
    int active;
    int pending;
    int priority;
    void *data;
    void (*cb)(struct ev_loop *loop, struct ev_embed *w, int revents);
    struct ev_loop *other;
    ev_io io;
    ev_prepare prepare;
    ev_check check;
    ev_timer timer;
    ev_periodic periodic;
    ev_idle idle;
    ev_fork fork;
    ev_cleanup cleanup;
} ev_embed;

typedef struct ev_async
{
    int active;
    int pending;
    int priority;
    void *data;
    void (*cb)(struct ev_loop *loop, struct ev_async *w, int revents);
    sig_atomic_t volatile sent;
} ev_async;

union ev_any_watcher
{
    struct ev_watcher w;
    struct ev_watcher_list wl;
    struct ev_io io;
    struct ev_timer timer;
    struct ev_periodic periodic;
    struct ev_signal signal;
    struct ev_child child;
    struct ev_stat stat;
    struct ev_idle idle;
    struct ev_prepare prepare;
    struct ev_check check;
    struct ev_fork fork;
    struct ev_cleanup cleanup;
    struct ev_embed embed;
    struct ev_async async;
};

int main ()
{
    fprintf(stdout, "aaa\n");
    return 0;
}
